package tech.yunland.shop.domain.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.moyuer.bsf.domain.model.AbstractVersionEntity;

import javax.persistence.Column;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import java.math.BigDecimal;

/**
 * 单据分录基类
 *
 * @author 袁首京<yuanshoujing@gmail.com>
 */
@MappedSuperclass
public abstract class BillEntry extends AbstractVersionEntity {

    protected static final long serialVersionUID = 4175132342899860662L;

    @ManyToOne
    @JSONField(serialize = false)
    protected Goods goods; // 商品

    @Column(nullable = false)
    protected BigDecimal price = BigDecimal.ZERO; // 单价

    @Column(nullable = false)
    protected BigDecimal quantity = BigDecimal.ZERO; // 数量

    @Column(nullable = false)
    protected BigDecimal amount = BigDecimal.ZERO; // 金额

    @Transient
    protected String goodsId;
    @Transient
    protected String goodsName;
    @Transient
    protected BigDecimal goodsPrice;
    @Transient
    protected String goodsPicturesName;
    @Transient
    protected BigDecimal goodsPrize;
    @Transient
    protected String goodsRemark;


    public String getGoodsRemark() {
        return goods != null ? this.goods.getRemark() : this.goodsRemark;
    }

    public void setGoodsRemark(String goodsRemark) {
        this.goodsRemark = goodsRemark;
    }

    public BigDecimal getGoodsPrize() {
        return goods != null ? this.goods.getPrize() : this.goodsPrize;
    }

    public void setGoodsPrize(BigDecimal goodsPrize) {
        this.goodsPrize = goodsPrize;
    }

    public BigDecimal getGoodsPrice() {
        return goods != null ? goods.getPrice() : this.goodsPrize;
    }

    public void setGoodsPrice(BigDecimal goodsPrice) {
        if (goodsPrice == null) {
            goodsPrice = BigDecimal.ZERO;
        }
        this.goodsPrice = goodsPrice;
    }

    public String getGoodsId() {
        if (goodsId != null) {
            return goodsId;
        }

        if (goods != null) {
            goodsId = goods.getId();
        }
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public String getGoodsName() {
        if (goodsName != null) {
            return goodsName;
        }

        if (goods != null) {
            goodsName = goods.getName();
        }
        return goodsName;
    }


    public String getGoodsPicturesName() {
        if (goodsPicturesName != null) {
            return goodsPicturesName;
        }

        if (goods != null) {
            goodsPicturesName = goods.getPictureName();
        }
        return goodsPicturesName;
    }

    public void setGoodsPicturesName(String goodsPicturesName) {
        this.goodsPicturesName = goodsPicturesName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public Goods getGoods() {
        return goods;
    }

    public void setGoods(Goods goods) {
        this.goods = goods;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        if (price == null) {
            price = BigDecimal.ZERO;
        }
        this.price = price;
    }

    public BigDecimal getQuantity() {
        return quantity;
    }

    public void setQuantity(BigDecimal quantity) {
        if (quantity == null) {
            quantity = BigDecimal.ZERO;
        }
        this.quantity = quantity;
    }

    public BigDecimal getAmount() {
        if (amount == null) {
            amount = quantity.multiply(price);
        }

        return amount;
    }

    public void setAmount(BigDecimal amount) {
        if (amount == null) {
            amount = BigDecimal.ZERO;
        }
        this.amount = amount;
    }
}
